import mitmproxy.http
from mitmproxy import ctx
import turtle

class Counter:
    def __init__(self):
        self.num = 0

    def request(self, flow: mitmproxy.http.HTTPFlow):
        if flow.request.host != "10.193.255.104":
            return
        print("request")
        # print(flow.request.text)
        t = flow.request.text

        # 仿真实验室2
        if "%E5%8D%9A%E8%BF%9C%E6%A5%BC%E4%BB%BF%E7%9C%9F%E5%AE%9E%E9%AA%8C%E5%AE%A4%E4%BA%8C" in t:
            t = t.replace(r"%E5%8D%9A%E8%BF%9C%E6%A5%BC%E4%BB%BF%E7%9C%9F%E5%AE%9E%E9%AA%8C%E5%AE%A4%E4%BA%8C",
            r"%B2%A9%D4%B6%C2%A5%B7%C2%D5%E6%CA%B5%D1%E9%CA%D2%B6%FE")
            print("仿真实验室二")

        if "%E5%8D%9A%E8%BF%9C%E6%A5%BC%E4%BB%BF%E7%9C%9F%E5%AE%9E%E9%AA%8C%E5%AE%A4" in t:
            t = t.replace(r"%E5%8D%9A%E8%BF%9C%E6%A5%BC%E4%BB%BF%E7%9C%9F%E5%AE%9E%E9%AA%8C%E5%AE%A4",
            r"%B2%A9%D4%B6%C2%A5%B7%C2%D5%E6%CA%B5%D1%E9%CA%D2")
            print("仿真实验室一")
        if "%E5%8D%9A%E8%BF%9C%E6%A5%BC" in t:
            t = t.replace(r"%E5%8D%9A%E8%BF%9C%E6%A5%BC", r"%B2%A9%D4%B6%C2%A5")
            print("博园楼")
        # 如果 博园楼
        # if "%E5%8D%9A%E8%BF%9C%E6%A5%BC" in t:
        #     print("博园楼")
        #     t= t.replace(r"%E5%8D%9A%E8%BF%9C%E6%A5%BC", r"%B2%A9%D4%B6%C2%A5")

        # elif "%B2%A9%D4%B6%C2%A5%E4%BB%BF%E7%9C%9F%E5%AE%9E%E9%AA%8C%E5%AE%A4" in t:
        #     print("仿真实验室")
        #     t = t.replace("%B2%A9%D4%B6%C2%A5%E4%BB%BF%E7%9C%9F%E5%AE%9E%E9%AA%8C%E5%AE%A4", "%B2%A9%D4%B6%C2%A5%B7%C2%D5%E6%CA%B5%D1%E9%CA%D2")
        flow.request.set_text(t)
        print(t)
            
    def response(self, flow: mitmproxy.http.HTTPFlow):
        if flow.request.host != "10.193.255.104":
            return
        if flow.response.status_code == 200:
        # print(flow.response.text)
            text = flow.response.text
            # print(text)
            text = text.replace("window.loginchat.deskip.value=GetIP()", "")
            text = text.replace("'window.loginchat.deskip.value=window.loginchat.deskcode2.value", "window.loginchat.deskip.value=window.loginchat.deskcode2.value")
            text = text.replace("GetIP=getuserip1.getip", "")
            # print(text)
            flow.response.set_text(text)
            print("修改")

addons = [
        Counter()
    ]